﻿using SelfMadeMillionaire.Core.Services;
using SelfMadeMillionaire.Web.Infrastructure.ViewModels.Home;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using PagedList;
using SelfMadeMillionaire.Web.Infrastructure.ViewModels.Education;
using SelfMadeMillionaire.Core.Specifications.Publications;
using SelfMadeMillionaire.Core.Enums;

namespace SelfMadeMillionaire.Web.Infrastructure.Controllers
{
	public class EducationController : BaseController
	{
		readonly IPublicationService _publicationService;
		readonly ISubscriberService _subscriberService;

		public EducationController(IPublicationService publicationService, ISubscriberService subscriberService)
		{
			_publicationService = publicationService;
			_subscriberService = subscriberService;
		}

		[HttpGet]
		public ActionResult Index(int? page)
		{
            ClearProductTypeInSession();

			int pageIndex = page ?? 1;
			int pageSize = 10;

			var model = new EducationListViewModel
			{
				Publications = _publicationService.GetList(new EducationSpecification(GetMinAccessLevel())).ToPagedList(pageIndex, pageSize),
			};

			if (Request.IsAjaxRequest())
				return PartialView("PublicationsList", model);

			return View(model);
		}

		private SubscriptionType GetMinAccessLevel()
		{
			var minAccessLevel = SubscriptionType.Free;
			if (Request.IsAuthenticated)
				minAccessLevel = _subscriberService.GetMinAccessLevel(User.Identity.Name);

			return minAccessLevel;
		}
	}
}
